﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
using Helper;
using SchoolManagementSystem.Objects;
using SchoolManagementSystem.Forms.BusinessForm;
using DevExpress.XtraEditors.Controls;
namespace SchoolManagementSystem.Details
{
    public partial class Receipt_VoucherLocalUC : DetailUC
    {

        public Receipt_VoucherLocalUC()
        {
            InitializeComponent();
            this.ObjectName = "phiếu thu nội bộ";
            this.Code = "ReceiptLocalBill";
            this.Type = new Voucher();


            //comboboxUC_PaymentMethod.Model = RegisterService.RegisterServices.PaymentMethodService;
            //comboboxUC_PaymentMethod.Uc = new Payment_MethodUC();
            //comboboxUC_PaymentMethod.ColumnMain = "Name";
            //comboboxUC_PaymentMethod.ColumnDisplay = "Code,Name";

            comboboxUC_ReceiptReason.Model = RegisterService.RegisterServices.PaymentReasonService;
            comboboxUC_ReceiptReason.Uc = new Receipt_Payment_ReasonUC(1);
            comboboxUC_ReceiptReason.SearchString = " Type=1";
            comboboxUC_ReceiptReason.ColumnMain = "Name";
            comboboxUC_ReceiptReason.ColumnDisplay = "Code,Name";


            comboboxUC_ReceiptReason.combobox.EditValueChanged += new EventHandler(lookUReceiptReason_EditValueChanged);

           
            comboboxUC_ReceiptReason.Enabled = false;

         //   dateEdit_ceatedate.Enabled = false;
          //  txtcode.Enabled = false;
            comboboxUC_ReceiptReason.ID = 6;
            dateEdit_ceatedate.DateTime = DateTime.Now;
            radioGroup_AutoMode.SelectedIndex = 1;
        }





        void lookUReceiptReason_EditValueChanged(object sender, EventArgs e)
        {
           
        }

      


        protected override long DetailDisplay()
        {
            comboboxUC_ReceiptReason.ID = 6;
            Voucher data = new Voucher();
            if (ID > 0)
            {
                data = (Voucher)RegisterService.RegisterServices.VoucherService.GetByID(ID);

            }
            else
            {
                data.CreateDate = DateTime.Now;
                if (radioGroup_AutoMode.SelectedIndex == 1)
                {
                    data.ReceiptNo = RegisterService.RegisterServices.VoucherService.GenerateReceiptNoLocal(data.CreateDate);
                }
            }
            txtcode.Text = data.ReceiptNo;
            txtnote.Text = data.Description;
            txtBillNo.Text = data.BillNo;
            radioGroup_moneytype.SelectedIndex = data.MoneyType;
            dateEdit_ceatedate.DateTime = data.CreateDate;
          //  comboboxUC_PaymentMethod.ID = data.PaymentMethod;
            if (data.ID == 0)
                radioGroup_paymentmethod.SelectedIndex = 0;
            else
                radioGroup_paymentmethod.SelectedIndex = (int)(data.PaymentMethod - 1);
            radioGroup_excuter.SelectedIndex = data.Excuter;
            if (ID > 0)
            {
                var local_voucher = RegisterService.RegisterServices.ReceiptVoucherLocalService.GetByVoucherID(data.ID);

                spinEdit_price.EditValue = local_voucher.Money;
                txtpayer.Text = local_voucher.Payer;
                txtaddress.Text = local_voucher.Address;
            }
            else
            {
               
                spinEdit_price.EditValue = 0;
            }
            txtnote.Text = data.Description;
             return ID;

        }

        protected override long Save()
        {
            Voucher data = new Voucher();
            if (ID > 0)
            {
                data = (Voucher)RegisterService.RegisterServices.VoucherService.GetByID(ID);
                data.CreateDate = dateEdit_ceatedate.DateTime;
            }
            else
            {
                data.CreateBy = ValueDefine.StaffID;
                data.CreateDate = dateEdit_ceatedate.DateTime;
            }
            data.BillNo = txtBillNo.Text;
           
            data.Description = txtnote.Text;
            data.ModifieBy = ValueDefine.StaffID;
            data.PaymentMethod = radioGroup_paymentmethod.SelectedIndex + 1; // comboboxUC_PaymentMethod.ID;
            data.ReceiptReason = comboboxUC_ReceiptReason.ID; //default =1
            data.MoneyType = radioGroup_moneytype.SelectedIndex;
            data.ModifieDate = DateTime.Now;
            data.Excuter = radioGroup_excuter.SelectedIndex;
            if (data.ID <= 0 && radioGroup_AutoMode.SelectedIndex == 1)
                data.ReceiptNo = RegisterService.RegisterServices.VoucherService.GenerateReceiptNoLocal(data.CreateDate);
            else
                data.ReceiptNo = txtcode.Text;
            ID = RegisterService.RegisterServices.VoucherService.Save(data, ref ErrorMess);

         
            var Local_voucher = new Receipt_VoucherLocal();
            if (data.ID > 0)
            {
                Local_voucher = RegisterService.RegisterServices.ReceiptVoucherLocalService.GetByVoucherID(ID);
            }
            else {
                Local_voucher.VoucherID = ID;
            }

            Local_voucher.Money = int.Parse(spinEdit_price.EditValue.ToString());
            Local_voucher.Payer = txtpayer.Text;
            Local_voucher.Address = txtaddress.Text;

            RegisterService.RegisterServices.ReceiptVoucherLocalService.Save(Local_voucher);

        


            return ID;
        }


        public override bool Delete(long SelectedID)
        {

            return RegisterService.RegisterServices.VoucherService.Delete(RegisterService.RegisterServices.VoucherService.GetByID(SelectedID));

        }

        private void ReceiptUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(groupControl1.Controls);
        }

        private void dateEdit_ceatedate_EditValueChanged(object sender, EventArgs e)
        {
            if (ID <= 0)
            {
                try
                {
                    txtcode.Text = RegisterService.RegisterServices.VoucherService.GenerateReceiptNoLocal(dateEdit_ceatedate.DateTime);
                }
                catch { }
            }

        }

        private void radioGroup_AutoMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtcode.Enabled = !(radioGroup_AutoMode.SelectedIndex == 1);
            if (radioGroup_AutoMode.SelectedIndex == 1 && ID <= 0)
            {
                txtcode.Text = RegisterService.RegisterServices.VoucherService.GenerateReceiptNoLocal(dateEdit_ceatedate.DateTime);
            }
        }

    }
}
